package saucaca.fragment;

import java.util.ArrayList;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.Adapter.ExpandableSongAdapter;
import saucaca.CustomView.CustomExpandableListView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.PlaylistObject;
import saucaca.Object.SongObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class OnlinePlayListSong extends Fragment {
	String TAG = OnlinePlayListSong.class.getSimpleName();
	MainActivity mActivity;
	
	String strKeyList;
	private CustomExpandableListView mView_listView;
	private View mView_EmptyLoading = null;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	ExpandableSongAdapter adapter;
	PlaylistObject ringtoneObject;

	ArrayList<SongObject> listSongs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		Bundle bundle = getArguments();
		ringtoneObject = (PlaylistObject) bundle.getSerializable(MainActivity.ID);
		View view = inflater.inflate(R.layout.playlist_online_songs, container, false);
		findViewById(view);
		mActivity.myActionBar.setMenu(ringtoneObject.getTXNAME(), false, false);
		return view;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(ringtoneObject.getTXNAME(), false, false);
	}

	private void findViewById(View view) {
		mView_listView = (CustomExpandableListView) view.findViewById(R.id.layoutlistview_ExpandListview);
		mView_listView.setPullLoadEnable(false);
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		new GetPlaylistSongs().execute("");
	}

	class GetPlaylistSongs extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.playlistDetails(ringtoneObject.getALBUM_ID());
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					listSongs = JsonReader.PlaylistDetails(result);
					if (listSongs.size() > 0) {
						adapter = new ExpandableSongAdapter(mActivity, mView_listView, listSongs,true);
						mView_listView.setAdapter(adapter);
					} else {
						progressBar.setVisibility(View.GONE);
						imgEmpty.setVisibility(View.VISIBLE);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public void onStart() {
		super.onStart();

	}

}
